<?php

namespace App\Admin\Controllers;

use App\Models\CaseProductField;
use App\Models\CaseProduct;
use Illuminate\Http\Request;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Admin;
use Dcat\Admin\Http\Controllers\AdminController;

class CaseProductFieldController extends AdminController
{
    public function __construct(Request $request)
    {
        $this->case_product_id = $request->case_product_id;
        return $this;
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CaseProductField(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            // $grid->column('case_product_id');
            $grid->column('type')->using([1 => '信息类', 2 => '附件类']);
            // $grid->column('fields');
            $grid->column('fields')->display(function ($fields) {
                $fields = array_column(json_decode($this->fields, true), 'name');
                $v='';
                foreach($fields as $k){
                    $v = $v."<span class='label' style='background:#586cb1'>$k</span> ";
                }
                return $v;
            });
            $grid->disableViewButton();
            $grid->disableFilterButton();
            $grid->disableCreateButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('case_product_id');
                $filter->expand(false);
            });
            $grid->tools([
                '<a href="'.admin_url('/caseproductfields/create?case_product_id='.$this->case_product_id).'" class="btn btn-primary grid-btn"><i class="feather icon-plus"></i><span class="d-none d-sm-inline">&nbsp;&nbsp;新增字段</span></a>',
            ]);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CaseProductField(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('case_product_id');
            $show->field('type');
            $show->field('fields');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        Admin::style(
            <<<CSS
    .has-many-fields .col-md-12 {padding-left: 0px;}
    .has-many-fields.table tr td:first-child, .table tr th:first-child {padding-left: 0;}
CSS
        );
        return Form::make(new CaseProductField(), function (Form $form) {
            $form->display('id');
            $form->text('name','区块名称')->required();
            $form->hidden('case_product_id')->value($this->case_product_id);
            $form->radio('type')->options([1 => '信息类', 2=> '附件类'])->default(1);
            $form->table('fields', function ($table) {
                $table->text('name','字段名称');
                $table->hidden('marker');
                $table->select('type','字段类型')->options([
                    'text' => '单行文本',
                    'textarea' => '多行文本',
                    'select' => '下拉单选',
                    'number' => '数字',
                    'switch' => '开关',
                    'radio' => '单选',
                    'checkbox' => '多选',
                    'multipleSelect' => '下拉多选',
                    'email' => '邮箱',
                    'url' => '网址',
                    'mobile' => '手机',
                    'time' => '时间',
                    'date' => '日期',
                    'datetime' => '时间日期',
                    ])->default('text');
                $table->text('default','默认值');
            })->saving(function ($v) {
                foreach($v as $key=>$value){
                    $v[$key]['marker'] = app('pinyin')->permalink($v[$key]['name']);
                }
                return json_encode($v);
            });
        });
    }
}
